# -*- coding: utf-8 -*-

#import ConfigParser
import sys
import codecs
from xlrd import open_workbook
from configobj import ConfigObj

def getValue(obj):
    if isinstance(obj, str):
        return obj.strip()
    elif isinstance(obj, unicode):
        #print 'getValue->', obj.encode("utf-8")
        return obj.encode("utf-8").strip() #unicode(obj)

def getCfg():
    ''' ConfigObj 真乃神器也，连逗号分割变成列表都做好了。   '''
    cfgData = ConfigObj("config.ini")
    print cfgData
    return cfgData



def checkValue(dataType, dataName, dataValue ):
    flag = False
    try:
        #print dataType, dataName, dataValue
        opts = cfgData.get(dataType)
        #print opts
        
        if opts :
            dn = getValue(dataName)
            temp = {}
            temp[dn] = 'dn'
            #print temp
            datas = opts.get(dn)
            #print datas
            if datas:
                # 如果INI文件里面只配置了一个项，那么，他就不是list，而是str，需处理一下
                if not isinstance(datas, list):
                    datas = [datas]
                    
                for data in datas:
                    #print data, getValue(dataValue)
                    if data == getValue(dataValue):
                        flag = flag or True
                    else:
                        flag = flag or False
            else:
                flag = True
        else:
            flag = True
    except Exception as e:
        print 'Error: ',sys.exc_info()[0], '\n'
    return flag

def check(vals):
    # 获取业务类型
    dataType = vals[5]
    return checkValue(dataType, vals[6], vals[8])
'''
    if 'AGPS' == dataType :
        return checkValue('AGPS', vals[6], vals[8])
        
    elif 'DMAPN' == dataType :
        return checkValue('DMAPN', vals[6], vals[8])
        
    elif 'DMAPN_CMWAP' == dataType :
        return checkValue('DMAPN_CMWAP', vals[6], vals[8])
        
    elif 'GPRS_CMNET' == dataType :
        return checkValue('GPRS_CMNET', vals[6], vals[8])
        
    elif 'GPRS_CMWAP' == dataType :
        return checkValue('GPRS_CMWAP', vals[6], vals[8])
        
    elif 'MMS' == dataType :
        return checkValue('MMS', vals[6], vals[8])
        
    elif 'PIM' == dataType :
        return checkValue('PIM', vals[6], vals[8])
        
    elif 'Streaming' == dataType :
        return checkValue('Streaming', vals[6], vals[8])
        
    elif 'WAP' == dataType :
        return checkValue('WAP', vals[6], vals[8])

    else :
        temp = ','.join(vals)
        print '错误的业务类型： ' + temp
        return 1
'''   
    

cfgData = getCfg()

wrongData = []

# 打开原始数据文件
wb = open_workbook('data\\data1.xls','rb')
#wb = open_workbook('test\\test.xls','rb')
# 遍历每个sheet
for s in wb.sheets():
    print 'Sheet:',s.name
    # 遍历每行
    for row in range(s.nrows):
        print row
        values=[]
        # 遍历每个单元格
        for col in range(s.ncols):
            values.append(unicode(s.cell(row,col).value))
            
        ret = check(values)
        if ret:
            pass
            #print ret
        else:
            temp = '\t'.join(values)

            wrongFile = codecs.open('wrong.txt','a','utf-8')
            #wrongFile.write(u'中文')
            wrongFile.write(temp + '\r\n')
            
            wrongFile.close()

            print temp
            

print '--------------'
for d in wrongData:
    print d
print '--------------'
